package org.myxdb.client.cmdtoken;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.xmlrpc.XmlRpcException;
import org.myxdb.client.cmdtoken.command.CreateDB;
import org.myxdb.client.cmdtoken.command.DeleteDB;
import org.myxdb.client.cmdtoken.command.Help;
import org.myxdb.client.cmdtoken.command.ListDB;
import org.myxdb.client.cmdtoken.command.RenameDB;

public class cmdtoken {
	private final static String[] tokenstr={"create","delete","rename","list","import","output","use","info","help","shutdown","close","database","document"};
	
	public static boolean token(String console)
	{
		String str="";
		
		int start=0;
		int end = 0;
		
		String first ="";
		String second ="";
		String third ="";
		String forth="";
		
		List list =  new ArrayList();
		
		for(int i=0; i<console.length();i++)
		{
			char current = console.charAt(i);
			if(current==' ') 
			{
				end = i;
				if(start ==0)
				{
					str = console.substring(start , end).trim();
					list.add(str);
					//System.out.println(str+"!!!");
				}
				else
				{
					str = console.substring(start+1 , end).trim();
					list.add(str);
					//System.out.println(str+"!!!");
				}													
				start = end;
				
			}
			else if(i==console.length()-1)
			{
				end = i; 
				str = console.substring(start, end+1).trim();
				list.add(str);
				//System.out.println(str+"!!!");
			}
		}
		for(int ii=0;ii<list.size();ii++)
		{
			System.out.println(list.get(ii));
		}
		if(list.size()==1)
		{
			first = (String)list.get(0);
			boolean flag =false;
			if(first.equalsIgnoreCase("info"))
			{
				System.out.println("info");
			}
			else if(first.equalsIgnoreCase("help"))
			{
				new Help().execute();
			}
			else if(first.equalsIgnoreCase("close"))
			{
				System.out.println("close");
			}
			else if(first.equalsIgnoreCase("quit"))
			{
				System.out.println("quit");
			}
			else
			{
				for(int k=0;k<tokenstr.length;k++)
				{
					if(first.equalsIgnoreCase(tokenstr[k]))
					{
						System.out.println("missing arguments");
						flag= true;
					}
				}
				if(flag == false)
				System.out.println("unkonwn command");
			}
		}
		else if(list.size()==2)
		{
			first = (String)list.get(0);
			second = (String)list.get(1);
			
		
			if(first.equalsIgnoreCase("list"))
			{	
				if(second.equalsIgnoreCase("database"))
				{					
					Object execute[] = new ListDB().execute();
					try {
						Map result =(Map)xmlrpc.client.execute("run", execute);
						if(result.size()==0)
						{
							System.out.println("no available database");
						}
						else
						{
							Iterator it = result.values().iterator();
							System.out.println("--------------");
							System.out.println("   database");
							System.out.println("--------------");
							System.out.println("");
							while(it.hasNext())
							{
								String r = (String)it.next();
								if(r!=null)
								System.out.println(r);
								else System.out.println("");
							}
							System.out.println("--------------");
						}
					} catch (XmlRpcException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}	
				}
				else if(second.equalsIgnoreCase("document"))
				{		
					System.out.println("list doc");	
				}
				else
				{	
					System.out.println("unknown symbol '"+second+"'");
				}
				
			}
			else if(first.equalsIgnoreCase("use"))
			{	
				
			}
			else if(first.equalsIgnoreCase("query"))
			{	
			
			}
			else
			{
				System.out.println("unknown command");
				
			}
		}
		else if(list.size()==3)
		{
			first = (String)list.get(0);
			second = (String)list.get(1);
			third =(String) list.get(2);
			
			if(first.equalsIgnoreCase("create"))
			{
				if(second.equalsIgnoreCase("database"))
				{
					Object execute[] = new CreateDB().execute(third);
					try {
						Map result =(Map)xmlrpc.client.execute("run", execute);
						System.out.println(result.get("result").toString());
					} catch (XmlRpcException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else if(second.equalsIgnoreCase("document"))
				{
					System.out.println("create doc");
				}
				else 
				{						
					System.out.println("unknown symbol '"+second+"'");
				}
			}
			else if(first.equalsIgnoreCase("delete"))
			{
				if(second.equalsIgnoreCase("database"))
				{						
					Object execute[] = new DeleteDB().execute(third);
					try {
						xmlrpc.client.execute("run", execute);
					} catch (XmlRpcException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else
				{						
					System.out.println("unknown symbol '"+second+"'");
				}
			}
			else if(first.equalsIgnoreCase("import"))
			{
				if(second.equalsIgnoreCase("document"))
				{		
					System.out.println("import doc");	
				}
				else
				{
					System.out.println("unknown symbol '"+second+"'");
				}
			}
			else if(first.equalsIgnoreCase("output"))
			{
				if(second.equalsIgnoreCase("document"))
				{	
					System.out.println("output doc");		
				}
				else
				{
					System.out.println("unknown symbol '"+second+"'");
				}
			}
			else 
			{	
				System.out.println("unknown command");
			}
		}
		else if(list.size()==4)
		{
			first = (String)list.get(0);
			second = (String)list.get(1);
			third =(String) list.get(2);
			forth =(String) list.get(3);
			if(first.equalsIgnoreCase("rename"))
			{
				if(second.equalsIgnoreCase("database"))
				{
					Object execute[] = new RenameDB().execute(third,forth);
					try {
						xmlrpc.client.execute("run", execute);
					} catch (XmlRpcException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else
				{
					System.out.println("unknown symbol '"+second+"'");
				}
			}
			else
			{
				System.out.println("unknown command");
			}
		}	
		else if(list.size()==0);
		else
		{
			System.out.println("too many arguments");
		}
		return true;
	}
}
